package com.db;

import java.util.ArrayList;

import android.content.Context;

import com.android.ocean.util.common.StringUtil;
import com.modle.User;

public class DBService {
	/**
	 * 
	 * @Description:TODO插入用户，判断有就不插入了
	 * @param@param context
	 * @param@param uid
	 * @param@param recetime
	 * @param@param u
	 * @param@return
	 * @return long
	 * @author zhuw
	 * @date 2013-8-22 下午5:05:57
	 */
	public static long inserOrUpdateUser(Context context, String uid,
			User fUser) {
		long result = 0;
		DBDao db = new DBDao(context);
		db.open();
		if (db.hasRecord(uid, fUser.id) == 0) {
			result = db.insertRecentUser(uid, StringUtil.getNowDate(), fUser);
		}else{
			
			result = db.updateRecentUser(uid, StringUtil.getNowDate(), fUser);

		}
		db.close();
		return result;

	}
	
	/**
	 * 
	 * @Description:TODO保存一系列
	 * @param@param context
	 * @param@param uid
	 * @param@param list
	 * @param@return
	 * @return long
	 * @author zhuw
	 * @date 2013-11-2 下午10:08:43
	 */
	public static long saveUserList(Context context, String uid,
			ArrayList<User> list) {
		long result = 0;
		DBDao db = new DBDao(context);
		db.open();
		
		for(User fUser:list){
			if (db.hasRecord(uid, fUser.id) == 0) {
				result = db.insertRecentUser(uid, StringUtil.getNowDate(), fUser);
			}else{
				
				result = db.updateRecentUser(uid, StringUtil.getNowDate(), fUser);

			}	
		}
		db.close();
		return result;

	}
	/**
	 * 
	 * @Description:TODO删除对应的用户
	 * @param@param context
	 * @param@param uid
	 * @param@param fid
	 * @return void
	 * @author zhuw
	 * @date 2013-8-22 下午5:05:41
	 */
	public static void delRecentUser(Context context, String uid,String fid) {
		DBDao db = new DBDao(context);
		db.open();
		db.delRecentUser(uid, fid);
		db.close();
		
	}
	/**
	 * 
	 * @Description:TODO主键删除
	 * @param@param context
	 * @param@param keyID
	 * @return void
	 * @author zhuw
	 * @date 2013-8-22 下午5:05:29
	 */
	public static void delRecentUser(Context context, int  keyID) {
		DBDao db = new DBDao(context);
		db.open();
		db.delRecentUser(keyID);
		db.close();
		
	}
	/**
	 * 
	 * @Description:TODO查询最近聊天的所有人记录
	 * @param@param context
	 * @param@param uid
	 * @param@return
	 * @return ArrayList<User>
	 * @author zhuw
	 * @date 2013-8-22 下午5:08:17
	 */
	public static  ArrayList<User> selectAllRecent(Context context,String uid){
		ArrayList<User> list=null;
		DBDao db = new DBDao(context);
		db.open();
		list=db.selectAllRecent(uid);
		db.close();
		return list;
		
	}
	/**
	 * 
	 * @Description:TODO根据类型搜索最近联系人
	 * @param@param context
	 * @param@param uid
	 * @param@param type
	 * @param@return
	 * @return ArrayList<User>
	 * @author zhuw
	 * @date 2013-8-24 下午7:22:44
	 */
	public static  ArrayList<User> selectAllRecentByType(Context context,String uid,String type){
		ArrayList<User> list=null;
		DBDao db = new DBDao(context);
		db.open();
		list=db.selectAllRecentByType(uid,type);
		db.close();
		return list;
		
	}
}
